package com.example.swm.jodd_mail;

import jodd.format.Printf;
import jodd.mail.*;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.List;

import static jodd.mail.CommonEmail.PRIORITY_HIGHEST;


@Component
public class joddMailUtil {
    public static void main(String[] args) {
//       send();
       get();
    }

    public static void send(){
        SmtpServer smtpServer = new SmtpServer("smtp.qq.com","1169501499@qq.com","cwizdsxpbxechfai");//邮箱的stmp服务器地址
        Email email = Email.create()
                .from("1169501499@qq.com") //发件人  from必须与上面的邮箱地址相同否则报错：553 Mail from must equal authorized user
                .to("1169501499@qq.com") //收件人地址
                .cc("1464035491@qq.com") //抄送人地址
                .subject("测试邮件") //主题
                .addHtml("<b>测试</b>"); //HTML格式的内容

        Email email1 = Email.create()
                .from("1169501499@qq.com")
                .to("adr1@jodd.org")//发送多人
                .to("adr2@jodd.org")
                .cc("xxx@bar.com")//抄送多人
                .cc("zzz@bar.com")
                .subject("Hello HTML!")
                .addHtml("<b>HTML</b> message")
                .priority(PRIORITY_HIGHEST);//设置邮件的优先级

        SendMailSession session = smtpServer.createSession();
        session.open();
        session.sendMail(email);
        session.close();
    }

    public static void get(){
        Pop3Server pop3Server = new Pop3Server("pop.qq.com",110,"1169501499@qq.com","cwizdsxpbxechfai");
        ReceiveMailSession session = pop3Server.createSession();
        session.open();
        //收到的邮件数量
        System.out.println(session.getMessageCount());
        //接收邮件
        ReceivedEmail[] emails = session.receiveEmailAndMarkSeen();
        if (emails != null) {
            for (ReceivedEmail email : emails) {
                System.out.println("\n\n===[" + email.getMessageNumber() + "]===");

                //发送人等相关信息
                System.out.println("%0x:"+ email.getFlags());
                System.out.println("FROM:" + email.getFrom());
                System.out.println("TO:" + email.getTo()[0]);
                System.out.println("SUBJECT:" + email.getSubject());
                System.out.println("PRIORITY:" + email.getPriority());
                System.out.println("SENT DATE:" + email.getSentDate());
                System.out.println("RECEIVED DATE: " + email.getReceiveDate());

                //邮件的相关信息
                List<EmailMessage> messages = email.getAllMessages();
                for (EmailMessage msg : messages) {
                    System.out.println("------");
                    System.out.println(msg.getEncoding());
                    System.out.println(msg.getMimeType());
                    System.out.println(msg.getContent());
                }

                //附件的相关信息
                List<EmailAttachment> attachments = email.getAttachments();
                if (attachments != null) {
                    System.out.println("+++++");
                    for (EmailAttachment attachment : attachments) {
                        System.out.println("name: " + attachment.getName());
                        System.out.println("cid: " + attachment.getContentId());
                        System.out.println("size: " + attachment.getSize());
                        attachment.writeToFile(
                                new File("d:\\", attachment.getName()));
                    }
                }
            }
        }
    }
}
